/**
 * @author n0skill
 */

var hitArray = [6, 5, 4, 3, 2, 1, 0, -1, -2, -3];
var woundArray = [2, 3, 4, 5, 6];
var p1shoot;
var p2shoot;
var p1hit;
var p2hit;
var p1wound;
var p2wound;
var p1realTA;
var p2realTA;
var numberOfWounds = [1];
var critical = false;
var typeofcritical;

function rollToWound(player, weapon) {
	rollDice(player)
	if(player === player1) {
		p1realStrength = player1.weaponStrength + modifierArray[weapon.fMod];
		p2realStrength = player2.weaponStrength + modifierArray[weapon.fMod];
		p2t = player2.t
		console.log("dado per ferire " + p1dice)

		if(p1realStrength - p2t == -2 || p1realStrength - p2t == -3) {
			woundArrayIndex = 4;
		}
		if(p1realStrength - p2t == -1) {
			woundArrayIndex = 3;
		}
		if(p1realStrength - p2t == 0) {
			woundArrayIndex = 2;
		}
		if(p1realStrength - p2t == 1) {
			woundArrayIndex = 1;
		}
		if(p1realStrength - p2t >= 2) {
			woundArrayIndex = 0;
		}
		if(p1realStrength + 3 < p2t) {
			console.log("unable to wound p2!")
		}
		p1wound = woundArray[woundArrayIndex];
		if(p1dice == 6 && p1wound < 6) {
			console.log('critico!!');
			rollDice(player1);
			if(p1dice == 1 || p1dice == 2) {
				critical = true;
				typeofcritical = 0;
				numberOfWounds = [1, 2];
			}
			if(p1dice == 3 || p1dice == 4) {
				critical = true;
				typeofcritical = 1;
				numberOfWounds = [1, 2];

			}
			if(p1dice == 5 || p1dice == 6) {
				critical = true;
				typeofcritical = 2;
				numberOfWounds = [1, 2];
			}
			console.log(".. di " + p1dice)
		}
		if(p1dice >= p1wound || critical) {
			if(critical) {
				for(i in numberOfWounds) {
					computeWound(player, weapon);

				}
				typeofcritical = -1;
				critical = false;
				numberOfWounds = [1];
			} else {
				computeWound(player, weapon);
			}
		} else {
			console.log("p2 not wounded!")
		}
	} else {
		p1realStrength = player1.weaponStrength + modifierArray[weapon.fMod];
		p2realStrength = player2.weaponStrength + modifierArray[weapon.fMod];
		console.log("dado per ferire " + p2dice)
		//critici...
		if(p2realStrength - player1.t == -2 || player2.weaponStrength - player1.t == -3) {
			woundArrayIndex = 4;
		}
		if(p2realStrength - player1.t == -1) {
			woundArrayIndex = 3;
		}
		if(p2realStrength - player1.t == 0) {
			woundArrayIndex = 2;
		}
		if(p2realStrength - player1.t == 1) {
			woundArrayIndex = 1;
		}
		if(p2realStrength - player1.t >= 2) {
			woundArrayIndex = 0;
		}
		if(p2realStrength + 3 < player1.t) {
			console.log("unable to wound p1!")
		}
		p2wound = woundArray[woundArrayIndex];
		if(p2dice == 6 && p2wound < 6) {
			console.log('critico!!');
			rollDice(player2);
			if(p2dice == 1 || p2dice == 2) {
				critical = true;
				typeofcritical = 0;
				numberOfWounds = [1, 2];
			}
			if(p2dice == 3 || p2dice == 4) {
				critical = true;
				typeofcritical = 1;
				numberOfWounds = [1, 2];

			}
			if(p2dice == 5 || p2dice == 6) {
				critical = true;
				typeofcritical = 2;
				numberOfWounds = [1, 2];
			}
			console.log(".. di " + p2dice)
		}
		if(p2dice >= p2wound || critical) {
			if(critical) {
				for(i in numberOfWounds) {
					computeWound(player, weapon);
				}
				typeofcritical = -1;
				critical = false;
				numberOfWounds = [1];
			} else {
				computeWound(player, weapon);
			}
		} else {
			console.log("p2 not wounded!")
		}
	}
}

function computeWound(player, weapon) {
	if(player === player1) {
		rollDice(player2)
		table = p1realStrength - 3
		if(table <= 0) {
			table = 0;
		}
		p2realTA = player2.TA + table - modifierArray[weapon.TAmod];
		if(typeofcritical == 1 || typeofcritical == 2) {
			wound(player, weapon);
		} else {
			if(p2dice < p2realTA) {
				console.log("TA fallito con " + p2dice)
				wound(player, weapon);
			} else {
				console.log("TA passato con " + p2dice)
			}
		}
	} else {
		rollDice(player1)
		table1 = p1realStrength - 3
		if(table1 <= 0) {
			table1 = 0;

		}
		p1realTA = player1.TA + table1 - modifierArray[weapon.TAmod];
		if(typeofcritical == 1 || typeofcritical == 2) {
			wound(player, weapon);
		} else {
			if(p1dice < p1realTA) {
				console.log("TA fallito con " + p1dice)
				wound(player, weapon);

			} else {
				console.log("TA passato con " + p1dice)
			}
		}
	}
}

function wound(player, weapon) {
	if(player === player1) {
		if(player2.w > 1) {
			player2.w -= 1
		} else {
			rollDice(player)
			if(typeofcritical == 2) {
				p1dice += 2;
			}
			console.log("tiro per killare " + p1dice)
			if(weapon.name == "hammer") {
				if(p1dice == 1) {
					player2.KD = true;
					player2.stunned = false;
					player2.KO = false;
				}
				if(p1dice >= 2 || p1dice <= 4) {
					player2.KD = false;
					player2.stunned = true;
					player2.KO = false;
				}
				if(p1dice > 4) {
					player2.w -= 1;
					player2.KD = false;
					player2.stunned = false;
					player2.KO = true;
				}
			} else {
				if(p1dice == 1 || p1dice == 2) {
					player2.KD = true;
					player2.stunned = false;
					player2.KO = false;
				}
				if(p1dice == 3 || p1dice == 4) {
					player2.KD = false;
					player2.stunned = true;
					player2.KO = false;
				}
				if(p1dice > 4) {
					player2.w -= 1;
					player2.KD = false;
					player2.stunned = false;
					player2.KO = true;
				}
			}
			console.log(player2.KD)
			console.log(player2.stunned)
			console.log(player2.KO)
		}
	} else {
		if(player1.w > 1) {
			player1.w -= 1
		} else {
			rollDice(player)
			if(typeofcritical == 2){
				p2dice += 2;
			}
			console.log("tiro per killare " + p2dice)
			console.log(weapon.name)
			if(weapon.name === "hammer") {
				if(p2dice == 1) {
					player1.KD = true;
					player1.stunned = false;
					player1.KO = false;
				}
				if(p2dice > 1 || p2dice < 5) {
					player1.KD = false;
					player1.stunned = true;
					player1.KO = false;
				}
				if(p2dice > 4) {
					player1.w -= 1;
					player1.KD = false;
					player1.stunned = false;
					player1.KO = true;
				}
			} else {
				if(p2dice == 1 || p2dice == 2) {
					player1.KD = true;
					player1.stunned = false;
					player1.KO = false;
				}
				if(p2dice == 3 || p2dice == 4) {
					player1.KD = false;
					player1.stunned = true;
					player1.KO = false;
				}
				if(p2dice > 4) {
					player1.w -= 1;
					player1.KD = false;
					player1.stunned = false;
					player1.KO = true;
				}
			}
			console.log(player1.KD)
			console.log(player1.stunned)
			console.log(player1.KO)
		}
	}
}